Add TAL-Reverb-II plugin to test
[juce-lv2.git] / juce / source / extras / the jucer / src / model / components / jucer_ImageButtonHandler.h
blobcf5afe73e83712a06c69f697460ef52b6bbdd464
1 /*
2 ==============================================================================
4 This file is part of the JUCE library - "Jules' Utility Class Extensions"
5 Copyright 2004-11 by Raw Material Software Ltd.
7 ------------------------------------------------------------------------------
9 JUCE can be redistributed and/or modified under the terms of the GNU General
10 Public License (Version 2), as published by the Free Software Foundation.
11 A copy of the license is included in the JUCE distribution, or can be found
12 online at www.gnu.org/licenses.
14 JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 ------------------------------------------------------------------------------
20 To release a closed-source product which uses JUCE, commercial licenses are
21 available: visit www.rawmaterialsoftware.com/juce for more information.
23 ==============================================================================
26 #ifndef __JUCER_IMAGEBUTTONHANDLER_JUCEHEADER__
27 #define __JUCER_IMAGEBUTTONHANDLER_JUCEHEADER__
29 #include "jucer_ButtonHandler.h"
30 #include "../../properties/jucer_ComponentColourProperty.h"
31 #include "../paintelements/jucer_ImageResourceProperty.h"
34 //==============================================================================
35 /**
37 class ImageButtonHandler : public ButtonHandler
39 public:
40 enum ImageRole
42 normalImage = 0,
43 overImage = 1,
44 downImage = 2
47 //==============================================================================
48 ImageButtonHandler()
49 : ButtonHandler ("Image Button", "ImageButton", typeid (ImageButton), 150, 24)
53 //==============================================================================
54 Component* createNewComponent (JucerDocument*)
56 return new ImageButton ("new button");
59 void getEditableProperties (Component* component, JucerDocument& document, Array <PropertyComponent*>& properties)
61 ButtonHandler::getEditableProperties (component, document, properties);
63 addColourProperties (component, document, properties);
65 ImageButton* const ib = (ImageButton*) component;
67 ComponentLayout& layout = *document.getComponentLayout();
69 properties.add (new ImageButtonProportionProperty (layout, ib));
71 properties.add (new ImageButtonResourceProperty (layout, ib, normalImage, "normal image"));
72 properties.add (new ImageButtonOpacityProperty (layout, ib, "opacity", normalImage));
73 properties.add (new ImageButtonColourProperty (layout, ib, "overlay col.", normalImage));
75 properties.add (new ImageButtonResourceProperty (layout, ib, overImage, "over image"));
76 properties.add (new ImageButtonOpacityProperty (layout, ib, "opacity", overImage));
77 properties.add (new ImageButtonColourProperty (layout, ib, "overlay col.", overImage));
79 properties.add (new ImageButtonResourceProperty (layout, ib, downImage, "down image"));
80 properties.add (new ImageButtonOpacityProperty (layout, ib, "opacity", downImage));
81 properties.add (new ImageButtonColourProperty (layout, ib, "overlay col.", downImage));
84 XmlElement* createXmlFor (Component* comp, const ComponentLayout* layout)
86 XmlElement* e = ButtonHandler::createXmlFor (comp, layout);
88 ImageButton* const ib = (ImageButton*) comp;
90 e->setAttribute ("keepProportions", doesImageKeepProportions (ib));
92 e->setAttribute ("resourceNormal", getImageResource (ib, normalImage));
93 e->setAttribute ("opacityNormal", getImageOpacity (ib, normalImage));
94 e->setAttribute ("colourNormal", getImageColour (ib, normalImage).toString());
96 e->setAttribute ("resourceOver", getImageResource (ib, overImage));
97 e->setAttribute ("opacityOver", getImageOpacity (ib, overImage));
98 e->setAttribute ("colourOver", getImageColour (ib, overImage).toString());
100 e->setAttribute ("resourceDown", getImageResource (ib, downImage));
101 e->setAttribute ("opacityDown", getImageOpacity (ib, downImage));
102 e->setAttribute ("colourDown", getImageColour (ib, downImage).toString());
104 return e;
107 bool restoreFromXml (const XmlElement& xml, Component* comp, const ComponentLayout* layout)
109 if (! ButtonHandler::restoreFromXml (xml, comp, layout))
110 return false;
112 ImageButton* const ib = (ImageButton*) comp;
113 ComponentLayout& l = const_cast <ComponentLayout&> (*layout);
115 setImageKeepProportions (l, ib, xml.getBoolAttribute ("keepProportions", true), false);
117 setImageResource (l, ib, normalImage, xml.getStringAttribute ("resourceNormal", String::empty), false);
118 setImageOpacity (l, ib, normalImage, (float) xml.getDoubleAttribute ("opacityNormal", 1.0f), false);
119 setImageColour (l, ib, normalImage, Colour::fromString (xml.getStringAttribute ("colourNormal", "0")), false);
121 setImageResource (l, ib, overImage, xml.getStringAttribute ("resourceOver", String::empty), false);
122 setImageOpacity (l, ib, overImage, (float) xml.getDoubleAttribute ("opacityOver", 1.0f), false);
123 setImageColour (l, ib, overImage, Colour::fromString (xml.getStringAttribute ("colourOver", "0")), false);
125 setImageResource (l, ib, downImage, xml.getStringAttribute ("resourceDown", String::empty), false);
126 setImageOpacity (l, ib, downImage, (float) xml.getDoubleAttribute ("opacityDown", 1.0f), false);
127 setImageColour (l, ib, downImage, Colour::fromString (xml.getStringAttribute ("colourDown", "0")), false);
129 return true;
132 void fillInCreationCode (GeneratedCode& code, Component* component, const String& memberVariableName)
134 ButtonHandler::fillInCreationCode (code, component, memberVariableName);
136 ImageButton* const ib = dynamic_cast <ImageButton*> (component);
138 String s;
140 s << getColourIntialisationCode (component, memberVariableName)
141 << '\n';
143 const String indent (String::repeatedString (" ", memberVariableName.length() + 13));
145 s << memberVariableName << "->setImages (false, true, "
146 << boolToString (doesImageKeepProportions (ib)) << ",\n"
147 << indent
148 << getImageCreationCode (ib, normalImage) << ", "
149 << valueToFloat (getImageOpacity (ib, normalImage)) << ", "
150 << colourToCode (getImageColour (ib, normalImage)) << ",\n"
151 << indent
152 << getImageCreationCode (ib, overImage) << ", "
153 << valueToFloat (getImageOpacity (ib, overImage)) << ", "
154 << colourToCode (getImageColour (ib, overImage)) << ",\n"
155 << indent
156 << getImageCreationCode (ib, downImage) << ", "
157 << valueToFloat (getImageOpacity (ib, downImage)) << ", "
158 << colourToCode (getImageColour (ib, downImage))
159 << ");\n";
161 code.constructorCode += s;
164 static const String getImageCreationCode (ImageButton* ib, const ImageRole role)
166 const String resName (getImageResource (ib, role));
168 if (resName.isEmpty())
169 return "Image()";
171 return "ImageCache::getFromMemory (" + resName + ", " + resName + "Size)";
174 //==============================================================================
175 class ImageButtonResourceProperty : public ImageResourceProperty <ImageButton>
177 public:
178 ImageButtonResourceProperty (ComponentLayout& layout_, ImageButton* const owner_, const ImageRole role_, const String& name)
179 : ImageResourceProperty <ImageButton> (*layout_.getDocument(), owner_, name, true),
180 role (role_),
181 layout (layout_)
185 //==============================================================================
186 void setResource (const String& newName)
188 setImageResource (layout, element, role, newName, true);
191 const String getResource() const
193 return getImageResource (element, role);
196 private:
197 const ImageRole role;
198 ComponentLayout& layout;
201 class SetImageResourceAction : public ComponentUndoableAction <ImageButton>
203 public:
204 SetImageResourceAction (ImageButton* const button,
205 ComponentLayout& layout_,
206 const ImageRole role_,
207 const String& newResource_)
208 : ComponentUndoableAction <ImageButton> (button, layout_),
209 newResource (newResource_),
210 role (role_),
211 layout (layout_)
213 oldResource = ImageButtonHandler::getImageResource (button, role_);
216 bool perform()
218 showCorrectTab();
219 ImageButtonHandler::setImageResource (layout, getComponent(), role, newResource, false);
220 return true;
223 bool undo()
225 showCorrectTab();
226 ImageButtonHandler::setImageResource (layout, getComponent(), role, oldResource, false);
227 return true;
230 private:
231 String newResource, oldResource;
232 const ImageRole role;
233 ComponentLayout& layout;
236 //==============================================================================
237 static void setImageResource (ComponentLayout& layout, ImageButton* button, const ImageRole role, const String& newName, const bool undoable)
239 jassert (role < 3);
241 if (role < 3 && getImageResource (button, role) != newName)
243 if (undoable)
245 layout.getDocument()->perform (new SetImageResourceAction (button, layout, role, newName),
246 "Change image resource");
248 else
250 button->getProperties().set ("resource" + String ((int) role), newName);
251 updateButtonImages (*layout.getDocument(), button);
252 layout.changed();
257 static const String getImageResource (ImageButton* button, const ImageRole role)
259 jassert (role < 3);
260 return button->getProperties() ["resource" + String ((int) role)].toString();
263 //==============================================================================
264 class SetImageKeepsPropAction : public ComponentUndoableAction <ImageButton>
266 public:
267 SetImageKeepsPropAction (ImageButton* const button,
268 ComponentLayout& layout_,
269 const bool newState_)
270 : ComponentUndoableAction <ImageButton> (button, layout_),
271 layout (layout_),
272 newState (newState_)
274 oldState = ImageButtonHandler::doesImageKeepProportions (button);
277 bool perform()
279 showCorrectTab();
280 ImageButtonHandler::setImageKeepProportions (layout, getComponent(), newState, false);
281 return true;
284 bool undo()
286 showCorrectTab();
287 ImageButtonHandler::setImageKeepProportions (layout, getComponent(), oldState, false);
288 return true;
291 private:
292 bool newState, oldState;
293 ComponentLayout& layout;
296 static bool doesImageKeepProportions (ImageButton* button)
298 return button->getProperties().getWithDefault ("keepImageProp", true);
301 static void setImageKeepProportions (ComponentLayout& layout, ImageButton* button, const bool newState, const bool undoable)
303 if (undoable)
305 layout.perform (new SetImageKeepsPropAction (button, layout, newState), "change imagebutton proportion mode");
307 else
309 button->getProperties().set ("keepImageProp", newState);
310 updateButtonImages (*layout.getDocument(), button);
311 layout.changed();
315 class ImageButtonProportionProperty : public ComponentBooleanProperty <ImageButton>
317 public:
318 ImageButtonProportionProperty (ComponentLayout& layout_, ImageButton* const owner_)
319 : ComponentBooleanProperty <ImageButton> ("proportional", "maintain image proportions", "scale to fit",
320 owner_, *layout_.getDocument()),
321 layout (layout_)
325 //==============================================================================
326 void setState (bool newState)
328 setImageKeepProportions (layout, component, newState, true);
331 bool getState() const
333 return doesImageKeepProportions (component);
336 private:
337 ComponentLayout& layout;
340 //==============================================================================
341 class SetImageOpacityAction : public ComponentUndoableAction <ImageButton>
343 public:
344 SetImageOpacityAction (ImageButton* const button,
345 ComponentLayout& layout_,
346 const ImageRole role_,
347 const float newState_)
348 : ComponentUndoableAction <ImageButton> (button, layout_),
349 role (role_),
350 layout (layout_),
351 newState (newState_)
353 oldState = ImageButtonHandler::getImageOpacity (button, role_);
356 bool perform()
358 showCorrectTab();
359 ImageButtonHandler::setImageOpacity (layout, getComponent(), role, newState, false);
360 return true;
363 bool undo()
365 showCorrectTab();
366 ImageButtonHandler::setImageOpacity (layout, getComponent(), role, oldState, false);
367 return true;
370 private:
371 const ImageRole role;
372 float newState, oldState;
373 ComponentLayout& layout;
376 static float getImageOpacity (ImageButton* button, const ImageRole role)
378 return (float) button->getProperties().getWithDefault ("imageOpacity" + String ((int) role), 1.0f);
381 static void setImageOpacity (ComponentLayout& layout, ImageButton* button, const ImageRole role, const float opacity, const bool undoable)
383 if (undoable)
385 layout.perform (new SetImageOpacityAction (button, layout, role, opacity), "change imagebutton opacity");
387 else
389 button->getProperties().set ("imageOpacity" + String ((int) role), opacity);
390 updateButtonImages (*layout.getDocument(), button);
391 layout.changed();
395 class ImageButtonOpacityProperty : public SliderPropertyComponent
397 public:
398 ImageButtonOpacityProperty (ComponentLayout& layout_, ImageButton* const owner_,
399 const String& name, const ImageRole role_)
400 : SliderPropertyComponent (name, 0.0, 1.0, 0.0),
401 owner (owner_),
402 layout (layout_),
403 role (role_)
407 //==============================================================================
408 void setValue (double newValue)
410 setImageOpacity (layout, owner, role, (float) newValue, true);
413 double getValue() const
415 return getImageOpacity (owner, role);
418 private:
419 ImageButton* const owner;
420 ComponentLayout& layout;
421 const ImageRole role;
424 //==============================================================================
425 class SetImageColourAction : public ComponentUndoableAction <ImageButton>
427 public:
428 SetImageColourAction (ImageButton* const button,
429 ComponentLayout& layout_,
430 const ImageRole role_,
431 const Colour& newState_)
432 : ComponentUndoableAction <ImageButton> (button, layout_),
433 role (role_),
434 layout (layout_),
435 newState (newState_)
437 oldState = ImageButtonHandler::getImageColour (button, role_);
440 bool perform()
442 showCorrectTab();
443 ImageButtonHandler::setImageColour (layout, getComponent(), role, newState, false);
444 return true;
447 bool undo()
449 showCorrectTab();
450 ImageButtonHandler::setImageColour (layout, getComponent(), role, oldState, false);
451 return true;
454 private:
455 const ImageRole role;
456 Colour newState, oldState;
457 ComponentLayout& layout;
460 static const Colour getImageColour (ImageButton* button, const ImageRole role)
462 return Colour::fromString (button->getProperties().getWithDefault ("imageColour" + String ((int) role), "0").toString());
465 static void setImageColour (ComponentLayout& layout, ImageButton* button, const ImageRole role, const Colour& colour, const bool undoable)
467 if (undoable)
469 layout.perform (new SetImageColourAction (button, layout, role, colour), "change imagebutton colour");
471 else
473 button->getProperties().set ("imageColour" + String ((int) role), colour.toString());
474 updateButtonImages (*layout.getDocument(), button);
475 layout.changed();
479 class ImageButtonColourProperty : public ColourPropertyComponent,
480 public ChangeListener
482 public:
483 ImageButtonColourProperty (ComponentLayout& layout_, ImageButton* const owner_,
484 const String& name, const ImageRole role_)
485 : ColourPropertyComponent (name, false),
486 owner (owner_),
487 layout (layout_),
488 role (role_)
490 layout_.getDocument()->addChangeListener (this);
493 ~ImageButtonColourProperty()
495 layout.getDocument()->removeChangeListener (this);
498 //==============================================================================
499 void setColour (const Colour& newColour)
501 setImageColour (layout, owner, role, newColour, true);
504 const Colour getColour() const
506 return getImageColour (owner, role);
509 void resetToDefault() {}
511 void changeListenerCallback (ChangeBroadcaster*)
513 refresh();
516 private:
517 ImageButton* const owner;
518 ComponentLayout& layout;
519 const ImageRole role;
522 //==============================================================================
523 static void updateButtonImages (JucerDocument& document, ImageButton* const ib)
525 Image norm = document.getResources().getImageFromCache (getImageResource (ib, normalImage));
526 Image over = document.getResources().getImageFromCache (getImageResource (ib, overImage));
527 Image down = document.getResources().getImageFromCache (getImageResource (ib, downImage));
529 ib->setImages (false, true, doesImageKeepProportions (ib),
530 norm,
531 getImageOpacity (ib, normalImage),
532 getImageColour (ib, normalImage),
533 over,
534 getImageOpacity (ib, overImage),
535 getImageColour (ib, overImage),
536 down,
537 getImageOpacity (ib, downImage),
538 getImageColour (ib, downImage));
543 #endif // __JUCER_IMAGEBUTTONHANDLER_JUCEHEADER__